package com.cartagena.financo.controller;

import java.util.List;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;

import com.cartagena.financo.model.Category;
import com.cartagena.financo.repository.CategoryRepository;
import com.cartagena.vraptor.extjs.ExtJSJson;

@Resource
public class CategoryController {
    
    private final Result result;
    private final CategoryRepository repository; 
    
    public CategoryController(Result result, CategoryRepository repository) {
        this.result = result; 
        this.repository = repository;
    }
    
    @Post
    @Path("/category/crud")
    public void save(Category category) {
        repository.add(category);
        this.result.use(ExtJSJson.class).from(category).success().serialize();
    }
    
    @Post
    @Path("/category/crud/{category.id}")
    public void update(Category category) {
        repository.update(category);
        this.result.use(ExtJSJson.class).from(category).success().serialize();
    }
    
    @Delete
    @Path("/category/crud/{category.id}")
    public void remove(Category category) {
        repository.remove(category);
        this.result.use(ExtJSJson.class).from(category).success().serialize();
    }   
    
    @Get
    @Path("/category/crud/{category.id}")
    public void get(Category category) {
        Category categories = repository.get(category.getId());
        this.result.use(ExtJSJson.class).from(categories).success().serialize();
    }

    @Get
    @Path("/category")
    public void listAll() {
        List<Category> categories = repository.listAll();
        this.result.use(ExtJSJson.class).from(categories).success().serialize();
    }
    

}
